COMP3141 Software System Design and Implementation

COMP3141: Software System Design and Implementation

Term 2, 2023

Quiz 7 (Week 7)

Table of Contents

1 Applicatives

1.1 Question 1

Which of the following type definitions are examples of Applicative?

  1. Maybe
  2. String
  3. (->) a for any a
  4. (,) a for any a
  5. [ ]
  6. Gen

1.2 Question 2

Consider the following data type definition for a non-empty list in Haskell.

data NonEmptyList a = One a | Cons a (NonEmptyList a)

Which of the following are law-abiding Applicative instances for NonEmptyList?

  1. instance Applicative NonEmptyList where
      pure x = Cons x (pure x)
      (One f) <*> (One x) = One (f x)
      (One f) <*> (Cons x _) = One (f x)
      (Cons f _) <*> (One x) = One (f x)
      (Cons f fs) <*> (Cons x xs) = Cons (f x) (fs <*> xs)
    
  2. instance Applicative NonEmptyList where
      pure x = One x
      (One f) <*> (One x) = One (f x)
      (One f) <*> (Cons x _) = One (f x)
      (Cons f _) <*> (One x) = One (f x)
      (Cons f fs) <*> (Cons x xs) = Cons (f x) (fs <*> xs)
    
  3. instance Applicative NonEmptyList where
      pure x = One x
      One f <*> xs = fmap f xs 
      (Cons f fs) <*> xs = fmap f xs `append` (fs <*> xs)
        where
         append (One x) ys = Cons x ys
         append (Cons x xs) ys = Cons x (xs `append` ys)
    
  4. instance Applicative NonEmptyList where
      pure x = Cons x (pure x)
      One f <*> xs = fmap f xs 
      (Cons f fs) <*> xs = fmap f xs `append` (fs <*> xs)
        where
         append (One x) ys = Cons x ys
         append (Cons x xs) ys = Cons x (xs `append` ys)
    

1.3 Question 3

Suppose I wanted to write a function pair, which takes two Applicative data structures and combines them in a tuple, of type:

pair :: (Applicative f) => f a -> f b -> f (a, b)

Select all correct implementations of pair.

  1. pair fa fb = pure fa <*> pure fb
    
  2. pair fa fb = pure (,) <*> pure fa <*> pure fb
    
  3. pair fa fb = pure (,) <*> fa <*> fb
    
  4. pair fa fb = fmap (,) fa <*> fb
    
  5. There is no way to implement this function.

2 Monads

2.1 Question 4

Which of the following type definitions are examples of Monad, or admit law-abiding Monad instances?

  1. Maybe
  2. String
  3. (->) a for any a
  4. (,) a for any a
  5. [ ]
  6. Gen

2.2 Question 5

We wish to write a function s of type [m a] -> m [a], for any monad m. It will unpack each given value of type m a and collect their results into a list. Which of the following is a correct implementation of this function?

  1. s :: Monad m => [m a] -> m [a]
    s [] = []
    s (a:as) = do
      x <- a
      xs <- s as
      return (x : xs)
    
  2. s :: Monad m => [m a] -> m [a]
    s [] = return []
    s (a:as) = do
      x <- a
      xs <- as
      return (x : xs)
    
  3. s :: Monad m => [m a] -> m [a]
    s [] = return []
    s (a:as) = do
      x <- a
      xs <- s as
      return (x : xs)
    
  4. s :: Monad m => [m a] -> m [a]
    s [] = return []
    s (a:as) = do
      a
      s as
      return (a : as)
    

3 Functor, Monad, Applicative

3.1 Question 6

Which of the following are correct definitions of the usual <*> operation for the type [a]?

  1. fs <*> xs = do
      f <- fs
      x <- xs
      return (f x)
    
  2. fs <*> xs = concatMap (flip map xs) fs
    
  3. fs <*> xs =
      fs >>= \f ->
      xs >>= \x ->
      [f x]
    
  4. fs <*> xs =
      fs >>= \f ->
      xs >>= \x ->
      f x
    

3.2 Question 7

Recall that two functions are considered equal if they have the same output for every input. Select all the true statements below.

  1. Kleisli composition is not well-defined in the Maybe monad.
  2. Every monad m and every function f :: a -> m a satisfies the equality f <=< f = f.
  3. Every instance of Applicative has to be an instance of Monad as well.
  4. Every instance of Monad has to be an instance of Functor as well.

3.3 Question 8

If a given unary type m is a Monad instance (and also a Functor and a Applicative instance), which of the following would definitely be correct implementations of fmap :: (a -> b) -> m a -> m b?

  1. fmap f [] = []
    fmap f (x:xs) = map f xs 
    
  2. fmap f = (pure f <*>)
    
  3. fmap f xs = f <*> xs
    
  4. fmap f xs =
      xs >>= \x -> return (f x) 
    

2023-08-13 Sun 12:51

Announcements RSS